package com.wang.myblog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.myblog.entity.Article;
import com.wang.myblog.entity.vo.ArticleVo;
import com.wang.myblog.service.ArticleService;
import com.wang.myblog.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 博客-文章 前端控制器
 * </p>
 *
 * @author s
 * @since 2021-05-21
 */
@Api(tags = {"博客/文章管理"}, description = "博客/文章管理")
@CrossOrigin
@RestController
@RequestMapping("/myblog/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @ApiOperation(value = "获取商品分页信息（当前登录用户超市）")
    @PostMapping(value = {"list/{current}/{size}"})
    public R list(@ApiParam(name = "current", value = "当前页", required = false, defaultValue = "1")
                  @PathVariable(value = "current", required = false) Integer current,
                  @ApiParam(name = "size", value = "每页显示条数", required = false, defaultValue = "10")
                  @PathVariable(value = "size", required = false) Integer size,
                  @ApiParam(name = "goods", value = "多条件查询对象", required = false)
                  @RequestBody ArticleVo article) {
        //超市id
        String shopId = "1";
        current = null == current ? 1 : current < 1 ? 1 : current;
        size = null == size ? 10 : size < 1 ? 10 : size;
        Page<Article> page = new Page<>(current, size);
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(article.getOrderBy()) || (!article.getOrderBy().equals("desc") && !article.getOrderBy().equals("asc"))) {//
            wrapper.orderByDesc("hot_article", "desc_id", "create_time");
        } else {
            wrapper.last("order by hot_article " + article.getOrderBy());
        }
        if (!StringUtils.isEmpty(article.getNickname())) {
            wrapper.like("nickname", article.getNickname());
        }
        if (!StringUtils.isEmpty(article.getTitle())) {
            wrapper.like("title", article.getTitle());
        }
        if (!StringUtils.isEmpty(article.getKeywords())) {
            wrapper.like("keywords", article.getDescription());
        }
        if (!StringUtils.isEmpty(article.getKeywords())) {
            wrapper.like("description", article.getDescription());
        }
        if (!StringUtils.isEmpty(article.getSublabelId())) {
            wrapper.like("sublabel_id", article.getSublabelId());
        }
        if (!StringUtils.isEmpty(article.getParlabelId())) {
            wrapper.like("parlabel_id", article.getParlabelId());
        }

        articleService.page(page, wrapper);
        return R.ok().data("total", page.getTotal()).data("size", page.getSize()).data("current", page.getCurrent())
                .data("pages", page.getPages()).data("records", page.getRecords());
    }
}

